var form = layui.form;
var layer = parent.layer === undefined ? layui.layer : top.layer;
var  $ = layui.jquery;
var table = layui.table;
function insert() {
    layer.open({
        type : 2,
        title : '添加字典',
        content : '/sysDictInfoController/toAddDict',
        shade : 0.5,
        area : ["500px","550px"],
        maxmin : true,
        end: function(){
            table.reload('roleListTable');
        }
    });
}
var active ={
    reload: function(){
        var demoReload = $('#demoReload');
        //执行重载
        layui.table.reload('roleListTable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                deliveryName: demoReload.val(),
                pid:$('#pid').val()
            }
        }, 'data');
    }
};
layui.jquery('#searchTransport').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
});
function delAll(){
    var checkStatus = layui.table.checkStatus('roleListTable'),
        data = checkStatus.data,
        roleId = [];
    if (data.length > 0) {
        for (var i in data) {
            roleId.push(data[i].id);
        }
        console.log(roleId.join(','));
        layer.confirm('确定删除选中的字典？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/sysDictInfoController/toDelete", {
                id: roleId.join(',')  //将需要删除的newsId作为参数传入
            }, function (data) {
                layer.msg("字典删除成功！", {icon: 2, time: 500});
                tableIns.reload();
                layer.close(index);
                window.location.reload();
            })
        })
    } else {
        layer.msg("请选择需要删除的字典");
    }
}
layui.use('table', function() {
    //监听表格复选框选择
    table.on('checkbox(demo)', function(obj) {
        console.log(obj)
    });
    //监听工具条
    table.on('tool(demo)', function(obj) {
        var data = obj.data;
        if (obj.event === 'tableUpdate') {
            window.UpdateId=data.id;
            layer.open({
                type : 2,
                title : '字典信息',
                content : '/sysDictInfoController/toUpdateDict?id='+data.id,
                shade : 0.5,
                area : ["500px","550px"],
                maxmin : true,
                end: function(){
                    table.reload('roleListTable');
                }
            });
        }
        else if (obj.event === 'tableDelete') {
            layer.confirm('真的删除行么', function(index) {
                obj.del();
                $.ajax({
                    type : "post",
                    url : "/sysDictInfoController/deleteDict?id=" + data.id,
                    dataType : "json"
                });
                layer.close(index);
            });
        }
    });
});
layui.use('table', function(){
    var table = layui.table;
    tableIns = table.render({
        elem: '#linkList'
        ,url:'/sysDictInfoController/selectDict'
        ,method: 'post' //默认：get请求
        ,request: {
            pageName: 'page' //页码的参数名称，默认：page
            ,limitName: 'limit' //每页数据量的参数名，默认：limit
        },where:{
            name:$("#name").val()
            ,pid:$("#pid").val()
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.totalRec, //解析数据长度
                "data": res.data.recList //解析数据列表
            };
        }
        ,cellMinWidth: 95
        ,cols: [[
            {type:'numbers'}
            ,{type: 'checkbox'}
            ,{field:'name', title:'名称', unresize: true,sort:true}
            ,{field:'mark', title:'标识',unresize: true,sort:true}
            ,{field:'describe', title:'描述',unresize: true,sort:true}
            ,{field:'createTime', title:'创建时间',unresize: true,align:'center',sort:true}
            ,{fixed: 'right', title:'操作',width:200, align:'center', toolbar: '#bar'}
        ]]
        ,page: true
        ,id:'roleListTable'
    });
});